package com.egov.business.common.service;

import com.egov.business.common.mapper.CommonMapper;
import com.egov.common.util.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.egov.config.annotation.dic.DicItem;
import javax.annotation.Resource;
import java.util.Map;

/**
 * @author denggang
 * @description: 公用操作业务层实现
 * @date 2019/5/13 11:25
 */
@Service
public class CommonService {
    @Resource
    private CommonMapper commonMapper;

    @Autowired
    private RedisCacheUtil redisUtil;

    //字典缓存过期时间 1个小时
    private static final Integer EXPIRE_TIME = 60 * 60;
    /**
     * 通用字典值翻译
     * @author denggang
     * @param map
     * @exception
     * @return DicItem
     * @date 2019/5/13 11:29
     */
    public DicItem dicTranslate(Map<String, Object> map) {
        //实体类字段的值
        String code = map.get("code").toString();
        //分组值
        String codeTypeValue = map.get("codeTypeValue").toString();
        String key = "";
        if (!StringUtils.isEmpty(codeTypeValue)) {
            key = "dic:" + map.get("valueName").toString() +":"+ codeTypeValue + ":" + code;
        } else {
            key = "dic:" + map.get("valueName").toString() +":"+ code;
        }

        DicItem dic = null;
        if (redisUtil.hasKey(key)) {
            dic = (DicItem)redisUtil.get(key);
        } else {
            String dicValue = commonMapper.dicTranslate(map);
            if (dicValue == null) {
                dic = new DicItem(code);
            } else {
                dic = new DicItem(code, dicValue);
                redisUtil.setex(key, dic, EXPIRE_TIME);
            }
        }
        if (dic.getCode() == null) {
            dic = null;
        }
        return dic;
    }
}
